#
# Copyright (C) 2013 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=ghostscript_lede
PKG_VERSION:=10.04.0
PKG_RELEASE:=1

PKG_SRC_NAME:=ghostpdl

PKG_MAINTAINER:=Tobias Waldvogel <tobias.waldvogel@gmail.com>
PKG_SOURCE:=$(PKG_SRC_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs10040/
PKG_HASH:=8b1594b067e00e386f818270a255eef6baba593197173725951d5f0c316dc205

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SRC_NAME)-$(PKG_VERSION)
PKG_BUILD_DEPENDS:=ghostscript_lede/host
PKG_FIXUP:=autoreconf
PKG_INSTALL:=1

HOST_PATCH_DIR := ./patches-host

# During its build Ghostscript needs to run some binaries of its
# own. Thus the 'host-build'
include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/package.mk

define Package/ghostscript_lede
  SECTION:=utils
  CATEGORY:=Printing
  TITLE:=(LEDE) ghostscript
  DEPENDS:=+cups_lede +libfreetype +fontconfig +libtiff \
	+libjpeg +libpng +libexpat +lcms2_lede +zlib +libcupsimage_lede
  SUBMENU:=Utilities
endef

define Package/ghostscript_lede/description
	Ghostscript, an interpreter for the PostScript language and for PDF.
endef

define Package/ghostscript_lede/config
config GS_ENABLE_CONTRIB_LEDE
	bool "Include contributed device drivers"
	default n

config GS_LARGE_COLOR_INDEX_LEDE
	bool "Use large color index (16bit instead of 8bit)"
	default n

config GS_EXTRA_DRIVERS_LEDE
	string "Additional devices drivers (comma separated)"
	default ""
endef

DRIVERS:=pdfwrite,ps2write,cups,pwgraster,appleraster,pclm,pclm8,pdfimage24,pdfimage8,pxlcolor,pxlmono
ifneq ($(CONFIG_GS_ENABLE_CONTRIB_LEDE),)
  DRIVERS:+=,$(CONFIG_GS_ENABLE_CONTRIB_LEDE)
endif

CONFIGURE_ARGS += \
	$(if $(CONFIG_GS_ENABLE_CONTRIB_LEDE),,--disable-contrib) \
	--with-large_color_index=$(if $(CONFIG_GS_LARGE_COLOR_INDEX_LEDE),1,0) \
	--with-fontpath=/usr/share/fonts \
	--enable-dynamic \
	--with-drivers=$(DRIVERS) \
	--disable-gtk \
	--disable-dbus \
	--disable-hidden-visibility \
	--without-libidn \
	--without-x \
	--without-tesseract \
	--without-versioned-path \
	--enable-fontconfig \
	--with-system-libtiff \
	--with-local-zlib=no \
	--enable-cups \
	--with-pdftoraster \
	--disable-compile-inits \
	--with-cups-serverbin="/usr/lib/cups" \
	--with-cups-serverroot="/etc/cups" \
	--with-cups-datadir="/usr/share/cups"

CONFIGURE_VARS += \
	PKGCONFIG=$(STAGING_DIR_HOST)/bin/pkg-config \
	CUPSCONFIG=$(STAGING_DIR)/host/bin/cups-config

define Build/Prepare
	$(call Build/Prepare/Default)
	# Remove local copies in order to use the system shared libs
	(cd $(PKG_BUILD_DIR) && $(RM) -r expat ijs jasper lcms2 lcms2mt lcms tesseract tiff jpeg libpng zlib freetype cups/libs)
endef

define Package/ghostscript_lede/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/* $(1)/usr/bin/
	$(INSTALL_DIR) $(1)/usr/share/ghostscript/lib
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/ghostscript/lib/* \
		$(1)/usr/share/ghostscript/lib/
	$(INSTALL_DIR) $(1)/usr/share/ghostscript/iccprofiles
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/iccprofiles/* \
		$(1)/usr/share/ghostscript/iccprofiles/
	$(CP) -r $(PKG_BUILD_DIR)/Resource $(1)/usr/share/ghostscript/
endef

$(eval $(call BuildPackage,ghostscript_lede))

